package com.schpro.webexperiment.util;

import com.schpro.webexperiment.model.Result;

/**
 * result util
 *
 * @author lss
 * @date 2019-02-27, Wed
 */
public class ResultUtils {


  /**
   * 封装失败的消息
   *
   * @param result  结果
   * @param message 提示消息
   */
  public static void packFailedResult(Result result, String message) {
    result.setSuccess(false);
    result.setMessage(message);
  }

  /**
   * 封装成功的消息
   *
   * @param result  结果
   * @param message 提示消息
   */
  public static void packSuccessfulResult(Result result, String message) {
    result.setSuccess(true);
    result.setMessage(message);
  }

  /**
   * 封装成功的消息
   *
   * @param result 结果
   */
  public static void packSuccessfulResult(Result result) {
    packSuccessfulResult(result, "操作成功");
  }


  /**
   * 简单拷贝结果
   *
   * @param src  源
   * @param dest 目的
   */
  public static void copyResult(Result src, Result dest, boolean withData) {
    dest.setSuccess(src.isSuccess());
    dest.setMessage(src.getMessage());
    if (withData) {
      //noinspection unchecked
      dest.setData(src.getData());
    }
  }


  /**
   * 返回失败模型
   *
   * @param msg 消息
   * @return Result
   */
  public static Result newFailedResult(String msg) {
    Result r = new Result();
    r.setSuccess(false);
    r.setMessage(msg);
    return r;
  }

  /**
   * 返回非法消息模型
   *
   * @return Result
   */
  public static Result newIllegalResult() {
    Result r = new Result();
    r.setSuccess(false);
    r.setMessage("非法操作！");
    return r;
  }

  /**
   * 返回成功模型
   *
   * @param msg  消息
   * @param data 数据
   * @return Result
   */
  public static <T> Result<T> newSuccessfulResult(String msg, T data) {
    Result<T> r = new Result<>();
    r.setSuccess(true);
    r.setMessage(msg);
    if (data != null) {
      r.setData(data);
    }
    return r;
  }

  /**
   * 返回成功模型
   *
   * @param data 数据
   * @return Result
   */
  public static <T> Result<T> newSuccessfulResult(T data) {
    return newSuccessfulResult("操作成功", data);
  }

  /**
   * 返回成功模型
   *
   * @return Result
   */
  public static <T> Result<T> newSuccessfulResult() {
    return newSuccessfulResult(null);
  }
}
